<?php
namespace app\api\controller;
use app\common\BaseController;
use app\common\Logs;
use app\common\MoneyLogs;
use app\api\model\AlreadyMonthly as AlreadyMonthlyModel;
use app\api\model\Receipt as ReceiptModel;
use think\Db;
class Receipt extends BaseController{    
    public function __construct(ReceiptModel $model){
        parent::__construct();
        $this->model = $model;
    }
    public function index(){        
        $params = $this->request->param();
        $filter=[
            'income_id'=>$params['income_id'],
            'type'=>$params['type']
        ];
        $data = $this->model->where($filter)->order('receipt_date', 'desc')->select();
        return $data;
    }
    //添加
    public function create(){        
        $params = $this->request->param();
        //type,1：团队，2：散拼，3：项目
        if(empty($params['type'])){
            return '缺少type参数';
        }
        if(empty($params['income_id'])){
            return '缺少income_id参数';
        }         
        $data = $this->model->allowField(true)->isUpdate(false)->data($params)->save();
        if($data){            
            if(in_array($params['type'],[1,2,3,10,16,17])){
                $data = Db::name("team_income")->alias('a')->join('lt_team b','a.parent_id=b.id')->where('a.id',$params['income_id'])->find();
            }else{
                $data = Db::name("single")->where(['type'=>$params['type'],'id'=>$params['income_id']])->find();
            }
            Logs::add("新增收款记录",$params["operator_id"],$data["sn"], null,json_encode($this->model,JSON_UNESCAPED_UNICODE));
            
            $am = AlreadyMonthlyModel::get(['year_month'=>date('Y-m', strtotime($params['receipt_date']))]);           
            if($am){
                MoneyLogs::add("增加收款",$params["operator_id"],$am['year_month'],$data["sn"], "",$this->model->money);
            }            
        }        
        return $this->model;
    }
    //添加
    public function create_all(){        
        $params = json_decode(file_get_contents('php://input'),true);
        $data = $this->model->allowField(true)->saveAll($params['list']);
        if($data){
            foreach($params['list'] as $v){
                if(in_array($v['type'],[1,2,3,10,16,17])){
                    $data = Db::name("team_income")->alias('a')->join('lt_team b','a.parent_id=b.id')->where('a.id',$v['income_id'])->find();
                }else{
                    $data = Db::name("single")->where(['type'=>$v['type'],'id'=>$v['income_id']])->find();
                }
                Logs::add("新增收款记录",$v["operator_id"],$data["sn"], null,json_encode($v,JSON_UNESCAPED_UNICODE));

                $am = AlreadyMonthlyModel::get(['year_month'=>date('Y-m', strtotime($v['receipt_date']))]);
                if($am){
                    MoneyLogs::add("增加收款",$params["operator_id"],$am['year_month'],$data["sn"], "",$v->money);
                }                   
            }
        }
        return $data;
    }
    public function batch_create(){        
        $params = $this->request->param();
        
        //type,1：团队，2：散拼，3：项目
        if(empty($params['type'])){
            return '缺少type参数';
        }
        if(empty($params['income_id'])){
            return '缺少income_id参数';
        }       
        $data = $this->model->allowField(true)->isUpdate(false)->data($params)->save();
        return $this->model;
    }
    //编辑
    public function update(){        
        $params = $this->request->param();
        $old = $this->model->where('id',$params['id'])->find();
        $data = $this->model->allowField(true)->isUpdate(true)->save($params,['id',$params['id']]); 
        if($data){            
            if(in_array($old['type'],[1,2,3,10,16,17])){
                $data = Db::name("team_income")->alias('a')->join('lt_team b','a.parent_id=b.id')->where('a.id',$old['income_id'])->find();
            }else{
                $data = Db::name("single")->where(['type'=>$old['type'],'id'=>$old['income_id']])->find();
            }
            Logs::add("修改收款记录",$params["operator_id"],$data["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));

            $am = AlreadyMonthlyModel::get(['year_month'=>date('Y-m', strtotime($old['receipt_date']))]);
            if($am){
                MoneyLogs::add("修改收款",$params["operator_id"],$am['year_month'],$data["sn"], $old['money'],$this->model->money);
            }
        }   
        return $this->model;
    }
    //删除
    public function delete(){
        $params = $this->request->param(); 
        $old = $this->model->where('id',$params['id'])->find();   
        $data = $this->model->allowField(true)->where('id',$params['id'])->delete();    
        if($data){            
            if(in_array($old['type'],[1,2,3,10,16,17])){
                $data = Db::name("team_income")->alias('a')->join('lt_team b','a.parent_id=b.id')->where('a.id',$old['income_id'])->find();
            }else{
                $data = Db::name("single")->where(['type'=>$old['type'],'id'=>$old['income_id']])->find();
            }
            Logs::add("删除收款记录",$params["operator_id"],$data["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));
            
            $am = AlreadyMonthlyModel::get(['year_month'=>date('Y-m', strtotime($old['receipt_date']))]);
            if($am){
                MoneyLogs::add("删除收款",$params["operator_id"],$am['year_month'],$data["sn"], $old['money'],$this->model->money);
            }
        }   
        return $data;
    }
  //审核
  public function review(){
    $params = $this->request->param();
    $old = $this->model->where('id',$params['id'])->find();  
    $data=$this->model->save(['is_review'=>'1'],['id'=>$params['id']]);
    if($data){        
        if(in_array($old['type'],[1,2,3,10,16,17])){
            $data = Db::name("team_income")->alias('a')->join('lt_team b','a.parent_id=b.id')->where('a.id',$old['income_id'])->find();
        }else{
            $data = Db::name("single")->where(['type'=>$old['type'],'id'=>$old['income_id']])->find();
        }
        Logs::add("审核收款记录",$params["operator_id"],$data["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));       
    }
    return $data;
}
//取消审核
public function cancel_review(){
    $params = $this->request->param();
    $old = $this->model->where('id',$params['id'])->find();  
    $data=$this->model->save(['is_review'=>'0'],['id'=>$params['id']]);
    if($data){        
        if(in_array($old['type'],[1,2,3,10,16,17])){
            $data = Db::name("team_income")->alias('a')->join('lt_team b','a.parent_id=b.id')->where('a.id',$old['income_id'])->find();
        }else{
            $data = Db::name("single")->where(['type'=>$old['type'],'id'=>$old['income_id']])->find();
        }
        Logs::add("解除审核收款记录",$params["operator_id"],$data["sn"], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));       
    }
    return $data;
}
}